﻿Public Class frmAD_ThemMoiTinBai

    Private Sub frmAD_ThemMoiTinBai_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cbSuaChua.Items.Add("1")
        cbSuaChua.Items.Add("2")
        cbSuaChua.Items.Add("3")
        cbSuaChua.Items.Add("4")
        cbSuaChua.Items.Add("5")
        cbSuaChua.Items.Add("6")
        cbSuaChua.Items.Add("7")
        cbSuaChua.Items.Add("8")
        cbSuaChua.Items.Add("9")
        cbSuaChua.Items.Add("10")
        cbSuaChua.SelectedIndex = 1
        Dim diachiDTO As New DiaChiDTO
        Dim diachiBUS As New DiaChiBUS

        Dim loainhaDTO As New LoaiNhaDTO
        Dim loaiNhaBUS As New LoaiNhaBUS

        Dim loaitinDTO As New LoaiTinDTO
        Dim loaitinBUS As New LoaiTinBUS

        Dim kvDTO As New KhuVucDTO
        Dim kvBUS As New KhuVucBUS

        tableLoaiTin = loaitinBUS.GetAll()
        tableLoaiNha = loaiNhaBUS.GetAll()
        tableKhuVuc = kvBUS.GetAll()

        For i As Integer = 0 To tableKhuVuc.Rows.Count - 1
            cbQuan.Items.Add(tableKhuVuc.Rows(i)("TenKhuVuc"))
        Next
        cbQuan.SelectedIndex = 4
        For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
            cbLoaiNha.Items.Add(tableLoaiNha.Rows(i)("TenLoaiNha"))
        Next
        cbLoaiNha.SelectedIndex = 0
        For i As Integer = 0 To tableLoaiTin.Rows.Count - 1
            cbLoaiTin.Items.Add(tableLoaiTin.Rows(i)("TenLoai"))
        Next
        cbLoaiTin.SelectedIndex = 1
    End Sub
    Public chunhaDTO As New ChuNhaDTO
    Public chunhaBUS As New ChuNhaBUS

    Private Sub btnThemMoi_Click(sender As System.Object, e As System.EventArgs) Handles btnThemMoi.Click
        If KiemTra() = True Then
            Dim nhaDTO As New DangTinDTO
            Dim nhaBUS As New DangTinBUS

            Dim diachiDTO As New DiaChiDTO
            Dim diachiBUS As New DiaChiBUS

            Dim loaitinDTO As New LoaiTinDTO
            Dim loaitinBUS As New LoaiTinBUS



            Dim taikhoanDTO As New TaiKhoanDTO
            '--------------------------------------------------------------------------------------------------------------------
            For i As Integer = 0 To tableLoaiTin.Rows.Count - 1
                If tableLoaiTin.Rows(i)("TenLoai").ToString() = cbLoaiTin.Text Then
                    nhaDTO.MaLoaiTin = Convert.ToInt32(tableLoaiTin.Rows(i)("MaLoaiTin").ToString())
                End If
            Next
            '--------------------------------------------------------------------------------------------------------------------
            For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
                If tableLoaiNha.Rows(i)("TenLoaiNha").ToString() = cbLoaiNha.Text Then
                    nhaDTO.MaLoaiNha = Convert.ToInt32(tableLoaiNha.Rows(i)("MaLoaiNha").ToString())
                End If
            Next
            '--------------------------------------------------------------------------------------------------------------------
            diachiDTO.Duong = txtDuong.Text
            diachiDTO.Phuong = txtPhuong.Text
            diachiDTO.Quan = cbQuan.Text
            diachiDTO.SoNha = txtSoNha.Text
            diachiDTO.MaDiaChi = 1
            diachiBUS.Insert(diachiDTO)
            diachiDTO = diachiBUS.GetItemID(txtSoNha.Text, txtDuong.Text, txtPhuong.Text, cbQuan.Text)
            '--------------------------------------------------------------------------------------------------------------------
            chunhaDTO = chunhaBUS.GetItemID(Taikhoan)
            '--------------------------------------------------------------------------------------------------------------------
            nhaDTO.TieuDe = txtTieuDe.Text
            nhaDTO.MaDangTin = 1
            nhaDTO.DienTich = Convert.ToSingle(txtDienTich.Text)
            nhaDTO.GiaThue = Convert.ToInt32(txtGiaThue.Text)
            nhaDTO.SucChua = Convert.ToInt32(cbSuaChua.Text)
            nhaDTO.ThoiGianDangTin = DateTime.Now.ToString("dd/MM/yyyy")
            Dim arr() As String = DateTime.Now.ToString("dd/MM/yyyy").Split("/")
            nhaDTO.ThoiGianDongTin = arr(0).ToString() + "/" + (Convert.ToInt32(arr(1).ToString) + 1).ToString() + "/" + arr(2).ToString()
            nhaDTO.MaDiaChi = diachiDTO.MaDiaChi
            nhaDTO.TinhTrang = 2
            nhaDTO.MaChuNha =Convert.ToInt32(txtChuNha.Text)
            '--------------------------------------------------------------------------------------------------------------------
            If ckXeBuyt.Checked = True Then
                nhaDTO.GanTramXeBuyt = 1
            Else
                nhaDTO.GanTramXeBuyt = 0
            End If
            If ckOGhep.Checked = True Then
                nhaDTO.OGhep = 1
            Else
                nhaDTO.OGhep = 0
            End If
            If ckNauAn.Checked = True Then
                nhaDTO.ChoNauAn = 1
            Else
                nhaDTO.ChoNauAn = 0
            End If
            If ckChungChu.Checked = True Then
                nhaDTO.OChungChu = 1
            Else
                nhaDTO.OChungChu = 0
            End If
            If ckInternet.Checked = True Then
                nhaDTO.Internet = 1
            Else
                nhaDTO.Internet = 0
            End If
            If ckTruyenHinhCap.Checked = True Then
                nhaDTO.TruyenHinhCap = 1
            Else
                nhaDTO.TruyenHinhCap = 0
            End If
            If ckGanCho.Checked = True Then
                nhaDTO.GanCho = 1
            Else
                nhaDTO.GanCho = 0
            End If
            If ckChoNamThue.Checked = True Then
                nhaDTO.ChoNamThue = 1
            Else
                nhaDTO.ChoNamThue = 0
            End If
            If ckChoNuThue.Checked = True Then
                nhaDTO.ChoNuThue = 1
            Else
                nhaDTO.ChoNuThue = 0
            End If
            '--------------------------------------------------------------------------------------------------------------------
            nhaBUS.Insert(nhaDTO)
            MessageBox.Show("Thành công", "Thông tin")
            Me.Close()
        Else
            MessageBox.Show("Phải nhập đầy đủ thông tin", "Thông tin")
        End If
    End Sub
    Private Function KiemTra() As Boolean
        If txtChuNha.Text = "" Or txtDienTich.Text = "" Or txtGiaThue.Text = "" Or txtSoNha.Text = "" Or txtSoNha.Text = "" Or txtDuong.Text = "" Or txtPhuong.Text = "" Or cbQuan.Text = "" Or cbSuaChua.Text = "" Then
            Return False
        End If
        Return True
    End Function
    Public tableLoaiNha As New DataTable
    Public tableChuNha As New DataTable
    Public tableLoaiTin As New DataTable
    Public tableKhuVuc As New DataTable
    Public _taikhoan As String
    Public Property Taikhoan() As String
        Get
            Return _taikhoan
        End Get
        Set(ByVal value As String)
            _taikhoan = value
        End Set
    End Property

    Private Sub SimpleButton1_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton1.Click
        tableChuNha = chunhaBUS.GetAll()
        dgv.DataSource = tableChuNha
    End Sub

    Private Sub dgv_RowEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.RowEnter
        Try
            chunhaDTO.MaChuNha = Convert.ToInt32(dgv.Rows(e.RowIndex).Cells("MaChuNha").Value.ToString())
            chunhaDTO.HoTen = dgv.Rows(e.RowIndex).Cells("HoTen").Value.ToString()
            txtChuNha.Text = Convert.ToInt32(dgv.Rows(e.RowIndex).Cells("MaChuNha").Value.ToString())
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtGiaThue_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaThue.KeyPress
        If (Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDienTich_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtDienTich.KeyPress
        If (Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub
End Class